<template>
  <div class="tabs">
    <el-tag
      v-for="(tag, index) in tags"
      :key="tag.title"
      :closable="tag.title !== '首页'"
      :effect="$route.meta.title === tag.title ? 'dark' : 'plain'"
      @click="changeMenu(tag)"
      @close="handleClose(tag, index)"
      size="small"
    >
      {{ tag.title }}
    </el-tag>
  </div>
</template>

<script>
import { mapState, mapMutations } from "vuex";
export default {
  name: "BreabTag",
  data() {
    return {};
  },
  computed: {
    ...mapState({
      tags: (state) => state.tab.tabsList,
    }),
  },
  methods: {
    ...mapMutations({
      close: "closeTag",
    }),
    changeMenu(item) {
      this.$router.push({ path: item.path });
    },
    handleClose(tag, index) {
      // 获取到最后一个tag的index值
      const length = this.tags.length - 1;
      this.close(tag);
      if (tag.title !== this.$route.title) {
        return;
      }
      if (index === length) {
        this.$router.push({
          path: this.tags[index - 1].path,
        });
      } else {
        this.$router.push({
          path: this.tags[index].path,
        });
      }
    },
  },
};
</script>

<style>
.tabs {
  margin-left: 10px;
}
.el-tag {
  margin-right: 15px;
  cursor: pointer;
}
</style>